package com.stylefeng.guns.modular.netcar.service.impl;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.modular.netcar.dao.OrderTypeMapper;
import com.stylefeng.guns.modular.netcar.model.OrderType;
import com.stylefeng.guns.modular.netcar.service.IOrderTypeService;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**

 * 订单类型 Service 实现 
 * @author 张亚林
 * @Date 2018-10-26 10:34
 */
@Service
public class OrderTypeServiceImpl extends ServiceImpl<OrderTypeMapper,OrderType> implements IOrderTypeService {

    @Resource
    private OrderTypeMapper orderTypeMapper;

    /**
     * 删除订单类型
     * @param orderType
     * @return     
     */
    @Transactional
    public int deleteOrderType(OrderType orderType,String token)throws Exception{
        if(orderType==null ){
            throw new RuntimeException("参数不全！");
        }
        return this.orderTypeMapper.deleteOrderType(orderType);
    }
    /**
     * 批量删除订单类型
     * @param ids 主键ID数组
     * @return 
     */
    @Transactional
    public int deleteBatchOrderType(String[] ids,String token)throws Exception{
        return this.orderTypeMapper.deleteBatchOrderType(ids);
    }
    /**
     * 查询订单类型列表
     * @param orderType 订单类型对象
     * @return
     */
    public List<OrderType> selectListOrderType(OrderType orderType,String token)throws Exception{
        return  this.orderTypeMapper.selectListOrderType(orderType);
    }
    /**
	 * 分页查询订单类型列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListOrderType(Pager pager,String token) throws Exception{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(orderTypeMapper.selectListOrderType(page,(OrderType)pager.getObj()));
        List<OrderType> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }
    /**
     * 同一个事务 互斥锁查询
     * @param orderType 订单类型对象
     * @return 
     */
    public  List<OrderType> selectListOrderTypeForUpdate(OrderType orderType)throws Exception{
        return  this.orderTypeMapper.selectListOrderTypeForUpdate(orderType);
    }
    /**
     * 通过主键id查询的订单类型列表
     * @param id 主键id
     * @return     
     */
    public  OrderType findOrderTypeById(String id,String token)throws Exception{
        return  this.orderTypeMapper.findOrderTypeById(id);
    }

    /**
     * 新增订单类型数据
     * @param orderType  订单类型对象
     * @return
     */
    @Transactional
    public int insertOrderType(OrderType orderType,String token) throws Exception { 
        if(orderType!=null && (orderType.getOtId()==null || String.valueOf(orderType.getOtId()).length()<10)){
            orderType.setOtId((int)IdWorker.getId());
        }  
        return this.orderTypeMapper.insertOrderType(orderType);
    }
    
    /**
     * 更新订单类型数据
     * @param orderType 订单类型对象
     * @return     
     */
    @Transactional
    public int updateOrderType(OrderType orderType,String token) throws Exception {     	
        return  this.orderTypeMapper.updateOrderType(orderType);
    }    
    /**
     * 查询订单类型返回key-Value属性
     * @param orderType 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsOrderType(OrderType orderType,String token) throws Exception {
        List<OrderType> listOrderType=this.orderTypeMapper.selectListOrderType(orderType);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(OrderType item:listOrderType){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getOtId());
            opts.setValue(item.getOtName());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
